package com.yanchangapp.ui.presenter;

import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.response.MailListResponse;
import com.yanchangapp.ui.view.MailListFgView;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MailListFgPresenter extends BasePresenter<MailListFgView> {

    public MailListFgPresenter(BaseActivity context) {
        super(context);
    }

    public void refreshMailList(String netMailType){
        HeadApiRetrofit.getInstance().queryMailListInfo(netMailType,1,"")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MailListResponse>() {
                    @Override
                    public void onNext(MailListResponse mailListResponse) {
                        if (mailListResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().refreshMailListSuccess(mailListResponse.getData().getList());
                            }
                        }else {
                            if (getView() != null){
                                getView().refreshMailListFailure(mailListResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().refreshMailListFailure("刷新列表失败");
                        }
                    }
                });
    }

    public void loadMoreMailList(String netMailType,int currentPage){
        HeadApiRetrofit.getInstance().queryMailListInfo(netMailType,currentPage,"")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MailListResponse>() {
                    @Override
                    public void onNext(MailListResponse mailListResponse) {
                        if (mailListResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().loadMoreListSuccess(mailListResponse.getData().getList());
                            }
                        }else {
                            if (getView() != null){
                                getView().loadMoreListFailure(mailListResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().loadMoreListFailure("加载邮件失败");
                        }
                    }
                });
    }

    public void actualDelete(List<Integer> selectLists){
        HeadApiRetrofit.getInstance().actualDeleteMail(selectLists)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().actualDeleteSuccess();
                            }
                        }else {
                            if (getView() != null){
                                getView().actualDeleteFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().actualDeleteFailure("彻底删除失败...");
                        }
                    }
                });

    }

    public void moveListToTargetList(String targetNetMail,List<Integer> selectLists){
        HeadApiRetrofit.getInstance().moveMailsToTargetMails(targetNetMail,selectLists)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().moveMailSuccess();
                            }
                        }else {
                            if (getView() != null){
                                getView().moveMailFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().moveMailFailure("移动邮件失败");
                        }
                    }
                });
    }


    public void setMailSeenState(int mailSeenState,List<Integer> selectLists){
        HeadApiRetrofit.getInstance().setMailSeenState(mailSeenState,selectLists)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().setSeenStateSuccess();
                            }
                        }else {
                            if (getView() != null){
                                getView().setSeenStateFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().setSeenStateFailure("设置邮件读取状态失败");
                        }
                    }
                });

    }
}
